﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using EntityObject;
using SharedLibraries;
using ThangNM_LIB.FUNC;

namespace DataAccessObject
{
    public class FileDAO : DB_SQL_EXT
    {
        public Boolean Insert_Update_Delete(ref FileEO _FileEO, Int16 Insert_Update_Delete)
        {
            SqlCommand cmd = new SqlCommand("File_Insert_Update_Delete");
            cmd.CommandType = CommandType.StoredProcedure;
            //Insert tblSEO
            setParamSEO(_FileEO, ref cmd);

            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", NullEx.getNullValue(_FileEO.FK_iAccountID)));
            cmd.Parameters.Add(new SqlParameter("@FK_sFileID", NullEx.getNullValue(_FileEO.FK_sFileID)));
            cmd.Parameters.Add(new SqlParameter("@sEmbed", NullEx.getNullValue(_FileEO.sEmbed)));
            cmd.Parameters.Add(new SqlParameter("@sSize", NullEx.getNullValue(_FileEO.sSize)));
            cmd.Parameters.Add(new SqlParameter("@sHomepage", NullEx.getNullValue(_FileEO.sHomepage)));
            cmd.Parameters.Add(new SqlParameter("@sLinkDownload", NullEx.getNullValue(_FileEO.sLinkDownload)));
            cmd.Parameters.Add(new SqlParameter("@bAdvertise", NullEx.getNullValue(_FileEO.bAdvertise)));
            cmd.Parameters.Add(new SqlParameter("@Insert_Update_Delete", Insert_Update_Delete));
            _FileEO.PK_iSEO =  ExecuteNonQuery_RetInt32(cmd);
            if (_FileEO.PK_iSEO > 0)
                return true;
            else
                return false;
        }

        //public  Boolean Update(FileEO _FileEO, SEOEO _SEOEO)
        //{
        //    SqlCommand cmd = new SqlCommand("File_Update", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iFileID", _FileEO.PK_iFileID));
        //    cmd.Parameters.Add(new SqlParameter("@sName", _FileEO.sName));
        //    //cmd.Parameters.Add(new SqlParameter("@sDescription", _FileEO.sDescription));
        //    cmd.Parameters.Add(new SqlParameter("@sRecommend", _FileEO.FK_sFileID));
        //    cmd.Parameters.Add(new SqlParameter("@sFileEx", _FileEO.sFileEx));
        //    cmd.Parameters.Add(new SqlParameter("@bAdvertise", _FileEO.bAdvertise));
        //    cmd.Parameters.Add(new SqlParameter("@iStatus", _FileEO.iState));
        //    //Insert tblSitemap
        //    cmd.Parameters.Add(new SqlParameter("@sUrlRewrite", NullEx.getNullValue(_SEOEO.sUrlRewrite)));
        //    cmd.Parameters.Add(new SqlParameter("@iChangefreq", NullEx.getNullValue(_SEOEO.iChangefreq)));
        //    cmd.Parameters.Add(new SqlParameter("@fPriority", _SEOEO.fPriority));
        //    //cmd.Parameters.Add(new SqlParameter("@iType", NullEx.getNullValue(_SEOEO.iType)));
        //    cmd.Parameters.Add(new SqlParameter("@bstatus", _SEOEO.bStatus));
        //    ExecuteNonQuery(cmd);
        //    return true;
        //}

        public Boolean Update_iQuantityDownload(Int32 _PK_iFileID)
        {
            SqlCommand cmd = new SqlCommand("File_Update_iQuantityDownload");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iFileID", _PK_iFileID));
            return ExecuteNonQuery_RetBool(cmd);
        }

        //public  Boolean Delete(string _PK_iFileID, String _loc_old)
        //{
        //    SqlCommand cmd = new SqlCommand("File_Delete", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iFileID", _PK_iFileID));
        //    cmd.Parameters.Add(new SqlParameter("@loc_old", _loc_old));
        //    ExecuteNonQuery(cmd);
        //    return true;
        //}

        public FileEO SelectItem(int _PK_iFileID)
        {
            SqlCommand cmd = new SqlCommand("File_SelectItem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iFileID", _PK_iFileID));
            return FileDO(GetDataTable(cmd));
        }

        public DataTable SelectBy_iState(Int16 _iState)
        {
            SqlCommand cmd = new SqlCommand("File_SelectBy_iState");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iState", NullEx.getNullValue(_iState)));
            return GetDataTable(cmd);
        }

        public DataSet SelectBy_PK_iFileID(Int32 _PK_iFileID)
        {
            SqlCommand cmd = new SqlCommand("File_SelectBy_PK_iFileID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iFileID", _PK_iFileID));
            return GetDataSet(cmd);
        }

        public FileEO FileDO(DataTable input)
        {
            List<FileEO> lst = new List<FileEO>();
            lst = (List<FileEO>)ConvertEx.ConvertDataTableToListObject<FileEO>(input, lst.GetType());
            if (CommonsCheck.ListObject_OK(lst))
                return lst[0];
            else
                return null;
        }
    }
}